package com.projectx.util;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.google.android.material.textfield.TextInputLayout;

import java.util.List;

public class MyAlertDialog{
    private int choice;
    /**
     * @param context 显示dialog的上下文
     * @param view dialog中的view
     * @param textInputLayout 布局
     * @param textView 需要更改信息的文本视图
     * @param title 对话框标题
     * @param hint 输入款提示
     */
    public void editTextDialog(@NonNull Context context, @NonNull View view, @NonNull final TextInputLayout textInputLayout, final TextView textView, @Nullable String title, @NonNull String hint){
        textInputLayout.setHint(hint);
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle(title);
        alert.setView(view);
        alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (textInputLayout.getEditText() != null){
                    textView.setText(textInputLayout.getEditText().getText().toString());
                }
        }
        });
        alert.setNegativeButton("取消",null);
        alert.show();
    }

    /**
     * 等待对话框
     * @param context
     * @param title
     * @return ,返回对话框
     */
    public ProgressDialog showProgressDialog(Context context,String title){
        final ProgressDialog progressDialog = new ProgressDialog(context);
        if (title == null){
            progressDialog.setTitle("请等待");
        } else {
            progressDialog.setTitle(title);
        }
        progressDialog.setMessage("等待中");
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.show();
        return progressDialog;
    }

    /**
     * 单选对话框
     * @param context
     * @param title
     * @param textView 要更改的文本
     * @param choices 单选列表
     * @param checkedIndex 初始选择的索引
     */
    public void singleChoiceDialog(Context context, String title, final String[] choices,final TextView textView,int checkedIndex){
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle(title);
        alert.setSingleChoiceItems(choices, checkedIndex, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                choice = which;
            }
        });
        alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                textView.setText(choices[choice]);
            }
        });
        alert.setNegativeButton("取消",null);
        alert.show();
    }
    //投票的单选对话框
    public void singleChoiceDialog(Context context, String title, final String[] choices, int checkedIndex){
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle(title);
        alert.setSingleChoiceItems(choices, checkedIndex, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                choice = which;
            }
        });
        alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               //更改票数
            }
        });
        alert.setNegativeButton("取消",null);
        alert.show();
    }
    public void DevoteDialog(Context context,View view, String title){
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle(title).setView(view);
        alert.setNegativeButton("取消",null);
        alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dialog.dismiss();
            }
        });
        alert.show();
    }
}
